package com.rcm.dda.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.springframework.jdbc.core.RowMapper;

public class RateKeyMapper implements RowMapper<RateKey> {

	public RateKey mapRow(ResultSet rs, int rowNum) throws SQLException {
		Integer warKeySeqNbr = null;

		// Create new RateKey
		RateKey rateKey = new RateKey(rs.getString("WARP_KEY_ID"),
				rs.getString("WARP_INS_LINE_CD"), rs.getString("WRI_KEY_DES"),
				rs.getString("WRI_FORM_LEVEL_CD"), new ArrayList<RateKeyAdd>());
		warKeySeqNbr = (Integer) rs.getObject("WAR_KEY_SEQ_NBR");
		if (!rs.wasNull()) {
			rateKey.getRateKeyAddList().add(
					new RateKeyAdd(rs.getInt("WAR_KEY_SEQ_NBR"), rs
							.getString("WAR_ADD_KEY_DES"), rs
							.getString("WAR_CON_IND")));
		}

		// Loop through ADD records
		while (rs.next()) {
			// Check if new Id then break and return the RateKey
			if (!rateKey.getWrapKeyId().equals(rs.getString("WARP_KEY_ID"))
					|| !rateKey.getWrapInsLineCd().equals(
							rs.getString("WARP_INS_LINE_CD"))) {
				rs.previous();
				break;
			}
			warKeySeqNbr = (Integer) rs.getObject("WAR_KEY_SEQ_NBR");
			if (!rs.wasNull()) {
				rateKey.getRateKeyAddList().add(
						new RateKeyAdd(warKeySeqNbr, rs
								.getString("WAR_ADD_KEY_DES"), rs
								.getString("WAR_CON_IND")));
			}
		}
		return rateKey;
	}
}
